package com.junxonline.plat.controller.server;

import com.junxonline.common.dao.vo.ResultVO;
import com.junxonline.common.util.CommonUtils;
import com.junxonline.plat.controller.base.BaseController;
import com.junxonline.plat.exception.ErrorCodeEnum;
import com.junxonline.plat.service.auth.LoginService;
import com.junxonline.plat.vo.LoginVO;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.spi.http.HttpContext;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @ClassName: ServerController
 * @Description: 服务器控制器
 * @author JunX
 */

@RestController
@RequestMapping(value = "/admin/server")
public class ServerController extends BaseController {

    @Value("${wms.soft.version}")
    private String serverVersion;

    @Value("${wms.soft.version.id}")
    private Long serverVersionId;

    @PostMapping(value = "/getServerInfo")
    public ResultVO getServerInfo() {

        Properties properties = System.getProperties();
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        // 获取服务器IP
        String serverIP = "";
        try {
            serverIP = InetAddress.getLocalHost().getHostAddress();
        } catch (Exception e) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        // 获取服务器名称
        StringBuilder serverName = new StringBuilder();
        serverName.append(request.getServerName());
        serverName.append("&nbsp;[");
        serverName.append(serverIP);
        serverName.append("]");

        // 获取服务器类型
        StringBuilder serverType = new StringBuilder();
        // 服务器操作系统名称
        serverType.append(properties.getProperty("os.name"));
        serverType.append("/");
        // 服务器操作系统架构
        serverType.append(properties.getProperty("os.arch"));
        serverType.append("/");
        // 服务器操作系统版本
        serverType.append(properties.getProperty("os.version"));

        // 获取服务器解译引擎
        String serverEngine = request.getServletContext().getServerInfo();

        // 获取服务器编码
        String serverEncording = properties.getProperty("file.encoding");

        // 获取服务器WEB端口
        String serverWebPort = String.valueOf(request.getServerPort());

        // 获取服务器JDK版本
        String serverJDKVersion = properties.getProperty("java.version");

        Map<String, Object> serverInfoMap = new HashMap<String, Object>();
        serverInfoMap.put("serverName", serverName.toString());
        serverInfoMap.put("serverType", serverType.toString());
        serverInfoMap.put("serverEngine", serverEngine);
        serverInfoMap.put("serverEncording", serverEncording);
        serverInfoMap.put("serverWebPort", serverWebPort);
        serverInfoMap.put("serverJDKVersion", serverJDKVersion);

        return getReturnSuccess(serverInfoMap);
    }

    @PostMapping(value = "/getServerVersion")
    public ResultVO getServerVersion() {

        // 获取服务器软件版本及版本ID
        String serverVersion = this.serverVersion;
        Long serverVersionId = this.serverVersionId;

        Map<String, Object> serverVersionMap = new HashMap<String, Object>();
        serverVersionMap.put("serverVersion", serverVersion);
        serverVersionMap.put("serverVersionId", serverVersionId);

        return getReturnSuccess(serverVersionMap);
    }

}
